import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import math

# 读取数据
data = pd.read_csv('result/all_results_cluster_0.4_v2.csv')

# 提取所需的数据列
metrics = data['Metric'].unique()
models = data['Model'].unique()
clusters = data['Cluster'].unique()

# 计算每个模型的索引
num_models = len(models)
model_indices = np.arange(num_models)

# 设置行数和列数
rows = 3
cols = math.ceil(len(metrics) / rows)

# 设置图形大小并创建子图
fig, axs = plt.subplots(rows, cols, figsize=(10 * cols, 6 * rows))

# 遍历每个指标
for i, metric in enumerate(metrics):
    # 在当前子图中绘制每个聚类类别的柱状图组
    ax = axs.flatten()[i]
    for j, cluster in enumerate(clusters):
        cluster_data = data[(data['Metric'] == metric) & (data['Cluster'] == cluster)]
        ax.bar(model_indices + j * 0.2, cluster_data['Value'], width=0.2, label=f'Cluster {cluster}')

    # 添加标题和标签
    ax.set_title(metric)
    ax.set_xlabel('Model')
    ax.set_ylabel('Value')
    ax.set_xticks(model_indices)
    ax.set_xticklabels(models)

    # 倾斜横坐标标签
    ax.set_xticklabels(models, rotation=45, ha='right')

    # 设置图例位置
    ax.legend(bbox_to_anchor=(1.02, 1), loc='upper left')

# 隐藏多余的空白子图
for i in range(len(metrics), rows*cols):
    fig.delaxes(axs.flatten()[i])

# 调整子图布局
plt.tight_layout()

# 显示图形
plt.show()


# 提高分辨率并保存图形
plt.savefig('chart/不同的cluster.png', dpi=300)

# 显示图形
plt.show()
